﻿#region
/**
 * $Date: 0001/1/1
*/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using BuXiaChu.OrderDishes.DAL.DataAccess;
using BuXiaChu.OrderDishes.Model;
#endregion

namespace BuXiaChu.OrderDishes.DAL
{
    public class NoticeBoardDAL
    {
        #region private const
        private const string PARAM_ID = "@Id";
        private const string PARAM_CONTENT = "@Content";
        private const string PARAM_LEVEL = "@Level";
        private const string PARAM_PUBLISHER_ID = "@PublisherId";
        private const string PARAM_PUBLISHER = "@Publisher";
        private const string PARAM_CREATE_DATE = "@CreateDate";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        private static void FillModelFrom(SqlDataReader reader, NoticeBoardModel obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.Id = reader.IsDBNull(reader.GetOrdinal("Id")) ? 0 : reader.GetInt32(reader.GetOrdinal("Id"));
                obj.Content = reader.IsDBNull(reader.GetOrdinal("Content")) ? String.Empty : reader.GetString(reader.GetOrdinal("Content"));
                obj.Level = reader.IsDBNull(reader.GetOrdinal("Level")) ? 0 : reader.GetInt32(reader.GetOrdinal("Level"));
                obj.PublisherId = reader.IsDBNull(reader.GetOrdinal("PublisherId")) ? 0 : reader.GetInt32(reader.GetOrdinal("PublisherId"));
                obj.Publisher = reader.IsDBNull(reader.GetOrdinal("Publisher")) ? String.Empty : reader.GetString(reader.GetOrdinal("Publisher"));
                obj.CreateDate = reader.IsDBNull(reader.GetOrdinal("CreateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("CreateDate"));
            }
            #endregion
        }

        private static SqlParameter[] GetNoticeBoardParams(NoticeBoardModel obj)
        {
            #region
            SqlParameter[] dbParams ={					
					 SQLHelper.MakeParam(PARAM_ID, SqlDbType.Int,0,obj.Id),
					 SQLHelper.MakeParam(PARAM_CONTENT, SqlDbType.NVarChar, 300,obj.Content),					
					 SQLHelper.MakeParam(PARAM_LEVEL, SqlDbType.Int,0,obj.Level),
					 SQLHelper.MakeParam(PARAM_PUBLISHER_ID, SqlDbType.Int,0,obj.PublisherId),
					 SQLHelper.MakeParam(PARAM_PUBLISHER, SqlDbType.NVarChar, 20,obj.Publisher)					
			};

            return dbParams;
            #endregion
        }

        public static bool Save(NoticeBoardModel obj)
        {
            #region
            try
            {
                SqlParameter[] dbParams = GetNoticeBoardParams(obj);

                if (obj.Id == 0)
                {
                    obj.Id = Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "NoticeBoard_InsertUpdate", dbParams));
                    return obj.Id > 0 ? true : false;
                }
                else
                {
                    var affectNum = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "NoticeBoard_InsertUpdate", dbParams);
                    return affectNum > 0 ? true : false;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("NoticeBoard-->InsertOrUpdate-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }

            return false;
            #endregion
        }

        public static NoticeBoardModel GetByNoticeBoardId(int id)
        {
            #region
            SqlDataReader reader = null;
            NoticeBoardModel obj = new NoticeBoardModel();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_ID, SqlDbType.Int, 0, id)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "NoticeBoard_GetByNoticeBoardId", dbParams);

                if (reader.Read())
                {
                    FillModelFrom(reader, obj);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("NoticeBoard-->GetSingleObject-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return obj;
            #endregion
        }

        public static bool Delete(int id)
        {
            #region
            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_ID, SqlDbType.Int, 0, id)
                    };

                return SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "NoticeBoard_Delete", dbParams) > 0 ? true : false;
            }
            catch (Exception e)
            {
                Exception ex = new Exception("NoticeBoard-->Delete-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            return false;
            #endregion
        }

        public static List<NoticeBoardModel> Search(string sqlWhere, int pageIndex, int pageSize, out int pageCount)
        {
            #region
            List<NoticeBoardModel> result = new List<NoticeBoardModel>();
            pageCount = 0;
            try
            {
                if (string.IsNullOrEmpty(sqlWhere))
                {
                    sqlWhere = " 1=1 ";
                }

                SqlParameter[] dbParams =
                {
                    SQLHelper.MakeParam("@sqlWhere", SqlDbType.NVarChar,0,sqlWhere),
                    SQLHelper.MakeParam("@pageIndex",SqlDbType.Int,0,pageIndex),
                    SQLHelper.MakeParam("@pageSize",SqlDbType.Int,0,pageSize),
                    SQLHelper.MakeOutParam("@pageCount", SqlDbType.Int,0,0)                    
                };
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "NoticeBoard_Search", dbParams))
                {
                    while (reader.Read())
                    {
                        NoticeBoardModel model = new NoticeBoardModel();
                        FillModelFrom(reader, model);
                        result.Add(model);
                    }
                }
                pageCount = int.Parse(dbParams[3].Value.ToString());
            }
            catch (Exception e)
            {
                Exception ex = new Exception("NoticeBoard-->Search-->" + e.ToString());
                ExceptionMessageDAL.Record(ex);
            }
            return result;
            #endregion
        }
    }
}
